<?php
defined('IN_PHPCMS') or exit('No permission resources.');
class index {
	function __construct() {
		pc_base::load_app_func('global');
		$siteid = isset($_GET['siteid']) ? intval($_GET['siteid']) : get_siteid();
  		define("SITEID",$siteid);
	}
	
	public function init() {
		$siteid = SITEID; 
 		$setting = getcache('guestbook', 'commons');
		$SEO = seo(SITEID, '', L('guestbook'), '', '');
		include template('guestbook', 'index');
	}
	
	 /**
	 *	留言板列表页
	 */
	public function list_type() {
		$siteid = SITEID;
  		$type_id = trim(urldecode($_GET['type_id']));
		$type_id = intval($type_id);
  		if($type_id==""){
 			$type_id ='0';
 		}
   		$setting = getcache('guestbook', 'commons');
		$SEO = seo(SITEID, '', L('guestbook'), '', '');
  		include template('guestbook', 'list_type');
	} 
 	
	 /**
	 *	留言板留言 
	 */
	public function register() { 
 		$siteid = SITEID;
        $isajax = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && 'xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH']);
 		if(isset($_REQUEST['dosubmit']) || $isajax){
 			if($_REQUEST['name']==""){
 				$this->show_message(100,L('usename_noempty'));
 			}
// 			if($_POST['lxqq']==""){
// 				$this->show_message(100,L('email_not_empty'));
// 			}
// 			if($_REQUEST['email']==""){
//                $this->show_message(100,L('email_not_empty'));
// 			}
            $mobile = $_REQUEST['shouji'];
            if(!preg_match('/^1([0-9]{10})$/',$mobile)){
                $this->show_message(100,'手机号码错误');
 			}
            $mobile_verify = $_REQUEST['code'];
            if(!$mobile_verify || preg_match("/[^a-z0-9]+/i",$mobile_verify)) {
                $this->show_message(100,'验证码错误');
            }

            //验证码
            $sms_report_db = pc_base::load_model('sms_report_model');
            $posttime = SYS_TIME-600;
            $where = "`mobile`='$mobile' AND `posttime`>'$posttime'";
            $r = $sms_report_db->get_one($where,'id,id_code','id DESC');
            if($r && $r['id_code']==$mobile_verify) {
                $sms_report_db->update(array('status'=>1),$where);
            } else {
                $this->show_message(100,'验证码错误');
            }

 			$guestbook_db = pc_base::load_model('guestbook_model');
 			 
			 /*添加用户数据*/
 			$sql = array('siteid'=>$siteid,'typeid'=>$_REQUEST['typeid'],'name'=>$_REQUEST['name'],'sex'=>$_REQUEST['sex'],'lxqq'=>$_REQUEST['lxqq'],'email'=>$_REQUEST['email'],'shouji'=>$_REQUEST['shouji'],'introduce'=>$_REQUEST['introduce'],'weixin'=>$_REQUEST['weixin'],'question'=>$_REQUEST['question'],'money'=>$_REQUEST['money'],'addtime'=>time());
 			 
 			$guestbook_db->insert($sql);
            $this->show_message(1,L('add_success'));
 		}else {
  			$setting = getcache('guestbook', 'commons');
 			if($setting[$siteid]['is_post']=='0'){
 				showmessage(L('suspend_application'), HTTP_REFERER);
 			}
 			$this->type = pc_base::load_model('type_model');
 			$types = $this->type->get_types($siteid);//获取站点下所有留言板分类
 			pc_base::load_sys_class('form', '', 0);
 			$setting = getcache('guestbook', 'commons');
 			$SEO = seo(SITEID, '', L('application_guestbook'), '', '');
   			include template('guestbook', 'register');
 		}
	}
    public function lists() {
        $siteid = SITEID;
        $setting = getcache('guestbook', 'commons');
        $SEO = seo(SITEID, '', L('guestbook'), '', '');
        include template('guestbook', 'lists');
    }
    public function show_message($code,$msg) {
        $isajax = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && 'xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH']);
        if($isajax)
        {
            echo json_encode([
                'code'=>$code,
                'msg'=>$msg
            ]);
            exit;
        }
        else
        {
            showmessage($msg);
        }
    }

}
?>